package org.mysocial.services.business.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.mysocial.datamodel.Account;
import org.mysocial.datamodel.Message;
import org.mysocial.datamodel.MessageDestination;
import org.mysocial.datamodel.MultipleDestinationMessage;
import org.mysocial.datamodel.SingleDestinationMessage;
import org.mysocial.services.business.MessageBusiness;
import org.mysocial.services.business.NotificationBusiness;
import org.mysocial.services.dao.MessageDAO;

/**
 * Session Bean implementation class MessageBusiness
 */
@Stateless
public class MessageBusinessImpl implements MessageBusiness
{
	@EJB
	private MessageDAO messageDAO;

	@EJB
	private NotificationBusiness notificationBusiness;

	@Override
	public SingleDestinationMessage sendMessage(String text, Account fromAccount,
	    Account toAccount)
	{
		SingleDestinationMessage message = new SingleDestinationMessage();
		message.setText(text);
		message.setSender(fromAccount);
		message.setReceiver(toAccount);
		message.setRead(false);
		message.setSentDate(new Date());

		message = (SingleDestinationMessage) messageDAO.persist(message);

		notificationBusiness.notifyMessage(message);

		return message;
	}

	@Override
	public MultipleDestinationMessage sendMessage(String text,
	    Account fromAccount, Account... toAccount)
	{
		MultipleDestinationMessage message = new MultipleDestinationMessage();
		message.setText(text);
		message.setSender(fromAccount);
		message.setSentDate(new Date());
		message.setReceivers(new ArrayList<MessageDestination>());

		MessageDestination destination;

		for (Account account : toAccount)
		{
			destination = new MessageDestination();
			destination.setReceiver(account);
			destination.setIsRead(false);

			message.getReceivers().add(destination);
		}

		message = (MultipleDestinationMessage) messageDAO.persist(message);

		notificationBusiness.notifyMessage(message);

		return message;
	}

	@Override
	public void markRead(Message message, Account account)
	{
		if (message instanceof SingleDestinationMessage)
		{
			((SingleDestinationMessage) message).setRead(true);
			return;
		} else
		{
			for (MessageDestination destination : ((MultipleDestinationMessage) message)
			    .getReceivers())
			{
				if (destination.getReceiver().equals(account))
					destination.setIsRead(true);
			}
		}
	}

	@Override
	public void remove(Message message)
	{
		messageDAO.remove(message);
	}

	@Override
	public SingleDestinationMessage reply(SingleDestinationMessage message,
	    String text)
	{
		return this.sendMessage(text, message.getReceiver(), message.getSender());
	}

	@Override
	public MultipleDestinationMessage reply(Account fromAccount,
	    MultipleDestinationMessage message, String text)
	{
		List<Account> destinations = new ArrayList<Account>();

		for (MessageDestination destination : message.getReceivers())
		{
			destinations.add(destination.getReceiver());
		}

		destinations.remove(fromAccount);
		destinations.add(message.getSender());

		return this.sendMessage(text, fromAccount,
		    destinations.toArray(new Account[destinations.size()]));
	}
}
